package cn.maszy.action.systemsetting;

import java.util.Map;


import net.sourceforge.stripes.action.SessionScope;
import org.springframework.beans.factory.annotation.Autowired;

import cn.maszy.common.BaseAction;
import cn.maszy.common.LoginFilter;
import cn.maszy.common.SessionFactory;
import cn.maszy.model.investment.B_T_Ufp_Work;
import cn.maszy.model.investment.B_T_Ufp_WorkExample;
import cn.maszy.model.systemsetting.C_V_Sys_LoginInfo;
import cn.maszy.service.projectmanagement.B_T_Ufp_TaskManagerService;
import cn.maszy.service.systemsetting.C_V_Sys_LoginInfoService;
import com.opensymphony.xwork2.ActionContext;

@SessionScope
public class LoginAction extends BaseAction<C_V_Sys_LoginInfo>{
	/**
	 * 版本控制信息
	 */
	private B_T_Ufp_Work b_T_Ufp_Work;
	private B_T_Ufp_WorkExample example;
	private static final long serialVersionUID = 6179604301571294802L;
	private C_V_Sys_LoginInfo c_V_Sys_LoginInfo;
	@Autowired
	private  C_V_Sys_LoginInfoService c_V_Sys_LoginInfoService;
	@Autowired
	private B_T_Ufp_TaskManagerService b_T_Ufp_TaskManagerService;
	private String dateString;
	//验证码
	private String code;
	//验证信息
	private String message;
	//session
	public Map<String, Object> sessionMap;
	private int count;
	private int sum;
	/*********************class set/get****************************/	
	
	public C_V_Sys_LoginInfo getC_V_Sys_LoginInfo() {
		return c_V_Sys_LoginInfo;
	}
	public B_T_Ufp_Work getB_T_Ufp_Work() {
		return b_T_Ufp_Work;
	}
	public void setB_T_Ufp_Work(B_T_Ufp_Work b_T_Ufp_Work) {
		this.b_T_Ufp_Work = b_T_Ufp_Work;
	}
	public void setC_V_Sys_LoginInfo(C_V_Sys_LoginInfo c_V_Sys_LoginInfo) {
		this.c_V_Sys_LoginInfo = c_V_Sys_LoginInfo;
	}
	public String getCode() {
		return code;
	}
	public B_T_Ufp_WorkExample getExample() {
		return example;
	}
	public void setExample(B_T_Ufp_WorkExample example) {
		this.example = example;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Map<String, Object> getSessionMap() {
		return sessionMap;
	}
	public void setSessionMap(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
	public String getDateString() {
		return dateString;
	}
	public void setDateString(String dateString) {
		this.dateString = dateString;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
	/*******************STRUTS ACTION METHOD*************************/
	/**
	 * 用户登录
	 * @throws Exception 
	 * @Author:Arthur
	 * @Time:2012-10-31
	 * @Parameter:@return
	 * @Return:String
	 * @Throws
	 */
	public String login() throws Exception{
		c_V_Sys_LoginInfo= c_V_Sys_LoginInfoService.loginQuery(c_V_Sys_LoginInfo);
		System.out.println(c_V_Sys_LoginInfo.getAdminId());
		if(c_V_Sys_LoginInfo.getAdminId() != null){
			/**
			 * 验证用户是否离职
			 */
			if("离职".equals(c_V_Sys_LoginInfo.getStatus())){
				message = "userError";
				return INPUT;
			}else{
			init();
			sessionMap.put(LoginFilter.USER_SESSION,c_V_Sys_LoginInfo);
			sessionMap.put("user", c_V_Sys_LoginInfo);
			//判断验证码
			if(!code.equals(sessionMap.get("rand").toString())){
				message="codeError";
				return INPUT;
			}else{
				/* 待接收的任务 */
				sum = b_T_Ufp_TaskManagerService.selectCount(SessionFactory.getSessionUserRealNam());
				/* 正在进行中的任务 */
				count = b_T_Ufp_TaskManagerService.queryCount(SessionFactory.getSessionUserRealNam());
				sessionMap.put("sum", sum);
				sessionMap.put("count", count);
				return SUCCESS;
			}
		}
		}else {
				message = "error";
			return INPUT;
		}
	}
	/**
	 * session初始化
	 * @Author:Arthur
	 * @Time:2012-11-1
	 * @Parameter:@return
	 * @Parameter:@throws Exception
	 * @Return:String
	 * @Throws
	 */
	public String init() throws Exception {
		// session初始化
		sessionMap = ActionContext.getContext().getSession();
		/*
		if (sessionMap != null && sessionMap.size() != 0) {
			// session清空处理
			sessionMap.clear();
		}*/
		
			return SUCCESS;
	}
	/**
	 * 注销session,用户登出
	 * @Author:Arthur
	 * @Time:2012-11-2
	 * @Parameter:@return
	 * @Parameter:@throws Exception
	 * @Return:String
	 * @Throws
	 */
	public String loginOut() {
		// 获取session内容
		Map<String, Object> sessionMap = ActionContext.getContext().getSession();
		if (sessionMap != null && sessionMap.size() != 0) {
			sessionMap.put(LoginFilter.USER_SESSION,null);
			// session清空处理
			sessionMap.clear();
		}
		return SUCCESS;
		
	}
}
